export const formatTime = (date = new Date(), format = 'YYYY-MM-DD H:I:S') => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const ymd = [year, month, day].map(formatNumber).join('-')
  const his = [hour, minute, second].map(formatNumber).join(':')

  if (format === 'YYYY-MM-DD') {
    return ymd
  } else if (format === 'H:I:S'){
    return his
  } else {
    return ymd + ' ' + his
  }
}

export const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export const unixTimestamp = (strtime = '') => {
  if (strtime) {
    var date = new Date(strtime);
  } else {
    var date = new Date();
  }
  //time1 = date.getTime();   //会精确到毫秒---长度为13位
  //time2 = date.valueOf(); //会精确到毫秒---长度为13位
  var time3 = parseInt(Date.parse(date) / 1000); //只能精确到秒
  return time3;
}
